package leecoke;


import java.util.ArrayList;
import java.util.List;

// 找出临界点之间的最小和最大距离
public class Test2058 {
    public int[] nodesBetweenCriticalPoints(ListNode head) {
        // 节点少于等于三个最多只有一个临界点，之间返回
        if (head == null || head.next == null || head.next.next == null) {
            return new int[]{-1, -1};
        }
        ArrayList<Integer> list = new ArrayList<>();
        ListNode node1 = head.next;
        ListNode node2 = head.next.next;
        while (node2 != null && node2.next != null) {
            ListNode node3 = node2.next;
            if ((node2.val > node1.val && node2.val < node2.next.val)
                    || (node2.val < node1.val && node2.val < node2.next.val)) {
                list.add(node2.val);
                node1 = node2;
                node2 = node3;
            }
        }

        int min = -1;
        for (int i = 0; i < list.size() - 1; i++) {
            min = Math.min(min, list.get(i + 1) - list.get(i));
        }
        int max = list.get(list.size() - 1) - list.get(0);
        return new int[]{min, max};
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
